<?php
// $Id$

/**
 * Controller_Group 控制器
 */
class Controller_Group extends Controller_Abstract
{
	function __construct($app){
		parent::__construct($app);
		$all_perms = Admin::get_all_perms();
		$this->_view['all_perms'] = $all_perms;
		$this->_view['subject'] = "用户组管理";
	}
	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
		$where="[isdelete]=0";
		$user = $this->_app->currentUser();
		$page = (int)$this->_context->page;
		if ($page==0) $page++;
		//echo $page;exit;
		$limit = $this->_context->limit ? $this->_context->limit : 15;
		$q = Group::find('[isdelete]=?',0)->limitPage($page, $limit);
		$this->_view['pager'] = $q->getPagination();
		$this->_view['list'] = $q->getAll();
		$this->_view['start'] = ($page-1)*$limit;
		$user = $this->_app->currentUser();
	}
	function actionCreate(){
		$this->_view['errorMessage'] = '';
		$alert = '';
		if ($this->_context->isPOST() && isset($_POST['groupname'])){
			$groupname = trim($_POST['groupname']);
			$perms = '';
			if(isset($_POST['perms'])){
				$perms = json_encode($_POST['perms']);
			}
			$args = Group::find('groupname=?&&isdelete=0',$groupname)->getAll();
			$num = count($args);
			if ($num>0) {
				$this->_view['errorMessage'] = '该用户组名称已经存在';
			} else{
				$form_value = array(
							'groupname'		=> $groupname,
							'perms'			=> $perms,
							);
				$group = new Group($form_value);
				$group->save();
				$alert = "<script language='javascript'>if(confirm('用户组添加成功，是否继续添加？')){window.open('".url('group/create')."','_self');}else{window.open('".url('group')."','_self');}</script>";
			}
		}
		$this->_view['alert'] = $alert;
	}
	function actionEdit(){
		$this->_view['errorMessage'] = '';
		$alert = '';		
		$id = $this->_context->id;
		$group = Group::find()->getById($id);	
		if ($this->_context->isPOST() && isset($_POST['groupname'])){
			$groupname = trim($_POST['groupname']);
			$perms = '';
			if(isset($_POST['perms'])){
				$perms = json_encode($_POST['perms']);
			}
			$args = Group::find('groupname=?&&isdelete=0&&id<>?',$groupname,$id)->getAll();
			$num = count($args);
			if ($num>0) {
				$this->_view['errorMessage'] = '该用户组名称已被其他用户组用了，请换一个';
			} else{
				$form_value = array(
								'id'			=> $id,
								'groupname'		=> $groupname,
								'perms'			=> $perms,
								);
				//print_r($form_value);
				$group -> changeProps($form_value);
				$group -> save();
				$alert = "<script language='javascript'>if(confirm('修改成功，是否继续修改？')){window.open('".url('group/edit',array('id'=>$id))."','_self');}else{window.open('".url('group')."','_self');}</script>";
			}
		}	
	 	$myData = $group->toArray();
	 	$myData['perms'] = json_decode($myData['perms'],true);
	 	//print_r($myData);
	 	$this->_view['myData'] = $myData;		
		$this->_view['alert'] = $alert;
	}
	function actionDelete(){
		// destroyWhere() 方法的参数和 find() 方法完全一致
		$group = Group::find('id = ?', $this->_context->id)->query();
	 	$group->isdelete = 1;
	 	$group->save();
		return $this->_redirect(url(''));
	}
}


